<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Servestooptions extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('servestooption');
	}
	public function ajax_servestooption_get(){
		$servestooptionEntity =  new CI_Serves_to_option_entity();
		$servestooption = $servestooptionEntity->getServesToOptionByCode($_REQUEST['code']);
		echo json_encode($servestooption);
	}
	public function ajax_servestooption_list(){
		$servestooptionEntity =  new CI_Serves_to_option_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$servestooptionList = $servestooptionEntity->getServesToOptionListByLanguage($defaultLanguageId);
		$data['servestooptionList'] = $servestooptionList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/servestooptions/index', $data);
	}
	public function ajax_servestooption_save(){
		$servestooptionNameList = $_REQUEST['servestooption'];
		$servestooptionNameDefault = '';
		foreach ($servestooptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$servestooptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($servestooptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_servestooption_require');
		}else{
			$code = 0;
			foreach ($servestooptionNameList as $key => $value) {
				$servestooptionEntity =  new CI_Serves_to_option_entity();
				if($value != null && $value != ''){
					$servestooptionEntity->setOption($value);
				}else{
					$servestooptionEntity->setOption($servestooptionNameDefault);
				}
				$servestooptionEntity->setLanguageId($key);
				$servestooptionEntity->setCode($code);
				$id = $servestooptionEntity->save();
				if($code == 0){
					$code = $id;
					$servestooptionEntity->setId($id);
					$servestooptionEntity->setCode($code);
					$servestooptionEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_servestooption_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_servestooption_update(){
		$servestooptionNameList = $_REQUEST['servestooption'];
		$servestooptionIdList = $_REQUEST['servestooption_id'];
		$servestooptionCode = $_REQUEST['servestooption_code'];
		$servestooptionNameDefault = '';
		foreach ($servestooptionNameList as $key => $value) {
			if($value != null && $value != ""){
				$servestooptionNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($servestooptionNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_servestooption_require');
		}else{
			foreach ($servestooptionNameList as $key => $value) {
				$servestooptionEntity =  new CI_Serves_to_option_entity();
				if($value != null && $value != ''){
					$servestooptionEntity->setOption($value);
				}else{
					$servestooptionEntity->setOption($servestooptionNameDefault);
				}
				$servestooptionEntity->setLanguageId($key);
				$servestooptionEntity->setCode($servestooptionCode);
				$servestooptionEntity->setId($servestooptionIdList[$key]);
				$servestooptionEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_servestooption_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_servestooption_delete(){
		$result = array();
		$servestooptionEntity =  new CI_Serves_to_option_entity();
		$result['result'] = $servestooptionEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_servestooption_delete_success');
		}else{
			$result['message'] = lang('escort_servestooption_delete_error');
		}
		echo json_encode($result);
	}
}
?>